How to use docker container create to prepare containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker create command to prepare Docker containers without immediately starting them. This allows you to configure containers and volumes before execution. You will begin by creating a container from the Ubuntu image, verifying its creation status, and then starting and attaching to the created container.

Furthermore, you will explore how to initialize a volume using docker create and subsequently utilize this initialized volume in another container. This hands-on experience will demonstrate the flexibility and control offered by the docker create command in managing container lifecycle and data persistence.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/start("Start Container") docker/ContainerOperationsGroup -.-> docker/attach("Attach to Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/run -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/ls -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/ps -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/start -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/attach -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/create -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/pull -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/images -.-> lab-555106{{"How to use docker container create to prepare containers"}} docker/volume -.-> lab-555106{{"How to use docker container create to prepare containers"}} end

Create a container without starting it

In this step, you will learn how to create a Docker container without immediately starting it. This is useful when you want to set up the container's configuration or volumes before it runs.

First, let's pull the ubuntu image from Docker Hub. This image will be used to create our container.

docker pull ubuntu:latest

You should see output indicating that the image is being downloaded. Once the download is complete, you can verify that the image is available locally by listing the images.

docker images

Now, we will create a container named my-ubuntu-container from the ubuntu image. We will use the docker create command for this. The -it flags are typically used for interactive containers, but docker create only sets up the container, it doesn't run it. We include them here for consistency with how you might eventually run the container.

docker create -it --name my-ubuntu-container ubuntu:latest

The docker create command creates a writeable container layer over the specified image and prepares it for running the specified command. It does not start the container. You should see a long string of characters as output, which is the container ID.

To verify that the container has been created but is not running, you can list all containers, including those that are stopped.

docker ps -a

In the output, you should see a container named my-ubuntu-container with a status of Created. This confirms that the container has been successfully created but is not yet running.

Start the created container and attach to it

In the previous step, you created a Docker container named my-ubuntu-container but did not start it. In this step, you will learn how to start this created container and attach your terminal to its standard input, output, and error streams.

To start the container, use the docker start command followed by the container name.

docker start my-ubuntu-container

This command starts the container. However, it runs in the background by default. To interact with the container, you need to attach to it.

To attach to the running container, use the docker attach command followed by the container name.

docker attach my-ubuntu-container

After running this command, your terminal will be connected to the container's shell. You should see a command prompt that looks like the one inside the Ubuntu container, typically something like root@<container-id>:/#.

Now that you are inside the container, you can execute commands within it. For example, let's check the operating system version.

lsb_release -a

You should see output showing the Ubuntu version running inside the container.

To exit the container without stopping it, press Ctrl + P followed by Ctrl + Q. This detaches your terminal from the container, leaving the container running in the background.

If you simply type exit or press Ctrl + D while attached, the container will stop.

After detaching, you can verify that the container is still running by listing the running containers.

docker ps

You should see my-ubuntu-container listed with a status indicating that it is running.

Initialize a volume using docker create

In this step, you will learn how to initialize a Docker volume using the docker create command. This method allows you to create a container specifically to populate a volume with initial data from the container's image.

First, let's create a new container named volume-initializer from the ubuntu image. This container will be used temporarily to initialize the volume. We will use the -v flag to specify the volume we want to initialize and its mount point inside the container. We'll name the volume my-initialized-volume and mount it at /app_data inside the container.

docker create -v my-initialized-volume:/app_data --name volume-initializer ubuntu:latest

This command creates the container and the volume if it doesn't already exist. The -v my-initialized-volume:/app_data part tells Docker to create a volume named my-initialized-volume and mount it to the /app_data directory inside the container.

Now, let's start this container. When a container with a volume is started for the first time, if the volume is empty, Docker copies the contents of the directory specified by the mount point from the image into the volume.

docker start volume-initializer

Since this container is just for initialization and doesn't have a long-running process, it will likely start and then immediately stop.

You can verify that the container has stopped by listing all containers.

docker ps -a

You should see volume-initializer with a status of Exited.

To confirm that the volume my-initialized-volume has been created, you can list all Docker volumes.

docker volume ls

You should see my-initialized-volume in the list of volumes.

At this point, the my-initialized-volume volume contains the initial data that was present in the /app_data directory of the ubuntu:latest image.

Use a volume initialized by docker create in another container

In the previous step, you initialized a Docker volume named my-initialized-volume by creating and starting a temporary container. Now, you will learn how to use this initialized volume in a different container. This demonstrates how volumes can persist data and be shared between containers.

First, let's create a new container named data-consumer from the ubuntu image. This time, we will directly run the container and mount the existing my-initialized-volume to the /data directory inside this new container.

docker run -it --name data-consumer -v my-initialized-volume:/data ubuntu:latest /bin/bash

Let's break down this command:

  • docker run: This command creates and starts a new container.
  • -it: These flags allocate a pseudo-TTY and keep stdin open, allowing for interactive use.
  • --name data-consumer: This assigns the name data-consumer to the new container.
  • -v my-initialized-volume:/data: This is the crucial part. It mounts the existing volume named my-initialized-volume to the /data directory inside the data-consumer container.
  • ubuntu:latest: This specifies the image to use for the container.
  • /bin/bash: This is the command to run inside the container, which starts a bash shell.

After executing this command, you will be attached to the bash shell inside the data-consumer container.

Now that you are inside the data-consumer container, navigate to the /data directory, which is where the my-initialized-volume is mounted.

cd /data

You can then list the contents of this directory to see the data that was initialized in the previous step. The exact contents will depend on what was in the /app_data directory of the ubuntu:latest image when the volume was initialized.

ls -l

You should see files and directories that were copied from the Ubuntu image's /app_data directory into the my-initialized-volume. This confirms that the volume was successfully initialized and is now being used by this new container.

To exit the data-consumer container, simply type exit.

exit

This will stop the data-consumer container and return you to your host machine's terminal.

You can verify that the data-consumer container has exited by listing all containers.

docker ps -a

You should see data-consumer with a status of Exited. The my-initialized-volume still exists and retains the data.

Summary

In this lab, you learned how to use the docker create command to prepare a Docker container without immediately starting it. This involved pulling an image, creating a container from that image using docker create, and verifying its "Created" status. You then learned how to start the created container and attach to it, demonstrating the separation between container creation and execution.

Furthermore, the lab covered initializing a Docker volume using docker create and subsequently utilizing this initialized volume in another container. This illustrated how docker create can be used for setting up infrastructure components like volumes before they are mounted and used by running containers.