Build and Run a Container
Explore how to build a Docker image from a Dockerfile, run containers interactively, and manage container lifecycles for Ansible development. This lesson helps you understand core Docker commands essential for creating a repeatable and flexible Ansible environment using containers.
We'll cover the following...
Build a Docker image
Before you run a container, you first have to create an image. You can achieve that by using the docker build command. docker build creates an image from the Dockerfile.
Connect to the terminal via the terminal widget, which can be found at the end of this lesson. Once connected, the contents of the current directory will be displayed. You can observe that the Dockerfile is available in the directory.
To further inspect the Dockerfile, you can use the following command:
This will open up the Dockerfile in an editor. Once reviewed, you can exit the nano editor by using Ctrl-X on Windows or Control+X on Mac.
Now, run the following command in the same directory as the Dockerfile:
You must be wondering what this flag -t ansible:latest does?
Docker uses tags to name and version container images. A tag consists of an image name and tag name. We provide the -t with the image and the tag name for that very purpose.
Once the build command has finished executing, run the following command:
You should see output similar to the one below but with a different image id.
| Repository | Tag | Image Id |
|---|---|---|
| ansible | latest | 56def654ec22 |
The Repository and the tag names are the same as provided in the
buildcommand.
Congratulations! You have been successful in creating an image. Let’s run it.
Run a Docker container
You can start a container from this newly built image. Use the docker run command to start a container.
Image Destroyed!
In case you are returning to this section at a later time than when you built the image, there is a chance that the image is destroyed, so you will have to run thebuildcommand again before executing theruncommand. Rundocker imagesto make sure the image exists.
Run the following command in the terminal:
Now, run the following command:
What do you see? The container is not listed, right?
A container needs to have a running process otherwise the container stops. Since the Ansible container isn’t doing anything, Docker stopped the container.
Run the command below:
This time you should see output similar to the one below, but a different container id than the one below:
| Container ID | Image | Status |
|---|---|---|
| 48bab7f673b3 | ansible | Exited |
Container Id
Docker assigns a randomly generatedContainer-Idto each container. Every time you run a container, a new random id will be generated for it.
Running the container interactively
You want to interact with the container rather than the container moving to the exited state as it does when it has no work to do. You can do that by passing a flag -it to run commands in the container.
Run the following command in the terminal to be able to interact with the container:
Once run, you will be able to notice that the prompt has changed from root@educative to root@<container-id>.
<container-id>will be replaced by the actual container id.
Run the following command within the container:
The installed version, 2.9.12 at the time of creating the course, will be printed on the screen. You can exit the container by running the exit command in the shell.
Disposable container
Once you have exited, the container goes into the stopped state. You can start it again and attach it to the container by using the following command:
However, containers are meant to be disposable. The idea is that you should be able to start a new one and not have to rely on any of the states that existed in the old container.
You can remove the container once it stops running using the following command:
Once the container exits, the container will be removed.
Click on the Run button and practice all the commands one by one in the terminal of the SPA widget. We have provided a summarized view of the commands below:
# Build an image from the Dockerfile with the tag -> ansible:latest docker build -t ansible:latest . # Display the images docker images # Run the Container docker run ansible # Display the running containers docker ps #Display all the containers docker ps --all # Run the Container in an Interactive terminal docker run -it ansible # Within the container, check if ansible is installed ansible --version # Exit the container's interactive terminal exit # Start a container that is currently in the exited state # Replace the <container-id> with the actual one docker start --attach <container-id> # Destroy the container after it exits docker run -it --rm ansible
In this lesson, we learned how to build, run, and manage a container using the following commands:
build: To build an image from aDockerfile.-t flag: To provide an image and tag name, used alongside thebuildcommand.run: To run a container from a pre-built image.ps: To display the running containers.-a or --allflag: To display all the containers. Used along with thepscommand.-it: To run containers in an interactive terminal. Used along with theruncommand.--rm: To remove the container once it stops.