Build and Run a Container

Get introduced to basic Docker commands to build an image and run a container.

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:

Press + to interact
nano Dockerfile

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:

Press + to interact
docker build -t ansible:latest .

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:

Press + to interact
docker images

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 build command.

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 the build command again before executing the run command. Run docker images to make sure the image exists.

Run the following command in the terminal:

Press + to interact
docker run ansible

Now, run the following command:

Press + to interact
docker ps

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:

Press + to interact
docker ps --all

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 generated Container-Id to 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:

Press + to interact
docker run -it ansible

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:

Press + to interact
ansible --version

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:

Press + to interact
# Replace the <container-id> with the actual one
docker start --attach <container-id>

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:

Press + to interact
docker run -it --rm ansible

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
Build and run a container

In this lesson, we learned how to build, run, and manage a container using the following commands:

  • build: To build an image from a Dockerfile.
  • -t flag: To provide an image and tag name, used alongside the build command.
  • run: To run a container from a pre-built image.
  • ps: To display the running containers.
  • -a or --all flag: To display all the containers. Used along with the ps command.
  • -it: To run containers in an interactive terminal. Used along with the run command.
  • --rm: To remove the container once it stops.

Get hands-on with 1300+ tech skills courses.