How to use docker compose create command to build and create containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker compose create command to build and create containers based on a docker-compose.yml file. We will begin by preparing a simple docker-compose.yml file, including the necessary steps to install Docker Compose in the LabEx environment.

Following the setup, you will explore the basic usage of docker compose create and then delve into its various options. This includes forcing container recreation with --force-recreate, creating containers without rebuilding images using --no-build, and ensuring the latest images are pulled with --pull always. By the end of this lab, you will have a solid understanding of how to leverage docker compose create for managing your multi-container applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555076{{"How to use docker compose create command to build and create containers"}} docker/ps -.-> lab-555076{{"How to use docker compose create command to build and create containers"}} docker/rm -.-> lab-555076{{"How to use docker compose create command to build and create containers"}} docker/create -.-> lab-555076{{"How to use docker compose create command to build and create containers"}} docker/pull -.-> lab-555076{{"How to use docker compose create command to build and create containers"}} end

Prepare a simple docker-compose.yml file

In this step, we will prepare a simple docker-compose.yml file. Before we start, let's install Docker Compose. Since the LabEx VM environment does not have Docker Compose pre-installed, we need to install it manually. We will download the Docker Compose binary and make it executable.

First, download the Docker Compose binary using curl. We will download version 1.29.2, which is compatible with the installed Docker version.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This command downloads the Docker Compose binary from the official GitHub releases page and saves it to /usr/local/bin/docker-compose. The $(uname -s) and $(uname -m) parts automatically detect your operating system and architecture, ensuring you download the correct binary.

Next, we need to make the downloaded binary executable.

sudo chmod +x /usr/local/bin/docker-compose

This command adds execute permissions to the docker-compose file, allowing you to run it as a command.

Now, let's verify that Docker Compose is installed correctly by checking its version.

docker-compose --version

You should see output similar to docker-compose version 1.29.2, build 5becea4c. This confirms that Docker Compose is installed and ready to use.

Now, let's create a simple docker-compose.yml file in your ~/project directory. This file will define a single service using the nginx image.

nano ~/project/docker-compose.yml

This command opens the nano text editor to create and edit the docker-compose.yml file. Paste the following content into the editor:

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

Let's break down this docker-compose.yml file:

  • version: '3.8' specifies the Docker Compose file format version.
  • services: defines the services (containers) that you want to run.
  • web: is the name of our service. You can choose any name you like.
  • image: nginx:latest specifies the Docker image to use for this service. In this case, we are using the latest version of the official Nginx image.
  • ports: maps ports between the host machine and the container. "80:80" maps port 80 on the host to port 80 in the container. This means you can access the Nginx web server running inside the container by visiting http://localhost in your web browser (or the VM's IP address).

Save the file by pressing Ctrl + O, then press Enter, and exit the editor by pressing Ctrl + X.

You have now successfully created a simple docker-compose.yml file that defines a web service using the Nginx image. In the next steps, we will use this file to create and manage containers.

Create containers using docker compose create

In this step, we will use the docker-compose create command to create containers based on the docker-compose.yml file we prepared in the previous step. The docker-compose create command creates the containers but does not start them. This is useful if you want to set up the container environment without immediately running the services.

Before creating the container, let's make sure the nginx image is available locally. If it's not, Docker Compose will pull it automatically during the create process. However, it's good practice to be aware of the images you are using. You can manually pull the image using the docker pull command:

docker pull nginx:latest

This command downloads the nginx:latest image from Docker Hub to your local machine. You should see output indicating the download progress and completion.

Now, navigate to the directory containing your docker-compose.yml file. In our case, it's the ~/project directory.

cd ~/project

Now, run the docker-compose create command:

docker-compose create

This command reads the docker-compose.yml file in the current directory and creates the containers defined in it. You should see output indicating that the container for the web service is being created.

After the command finishes, you can check the status of the created containers using the docker ps -a command.

docker ps -a

The output of docker ps -a will show all containers, including those that are not running. You should see a container named project_web_1 (the name is generated based on the directory name and service name) with a status of Created. This confirms that the container has been successfully created but is not yet started.

The docker-compose create command is useful for pre-provisioning your containers. In the next steps, we will explore how to start and manage these containers.

Force recreate containers with --force-recreate

In this step, we will learn how to force recreate containers using the docker-compose create --force-recreate command. The --force-recreate flag is used to recreate containers even if their configuration has not changed. This is useful in scenarios where you might need to refresh the container instance, for example, after making changes to the underlying image that Docker Compose might not automatically detect.

First, ensure you are in the ~/project directory where your docker-compose.yml file is located.

cd ~/project

Now, run the docker-compose create command with the --force-recreate flag:

docker-compose create --force-recreate

Since we already created the container in the previous step, Docker Compose will detect the existing container. Because of the --force-recreate flag, it will remove the existing container and create a new one with the same configuration. You should see output indicating that the existing container is being removed and a new one is being created.

Let's verify the status of the containers again using docker ps -a.

docker ps -a

You should still see a container named project_web_1 with the status Created. Although the name and configuration are the same, this is a new container instance that was created because of the --force-recreate flag.

The --force-recreate flag is a powerful option when you need to ensure a fresh instance of your container, regardless of whether the configuration in the docker-compose.yml file has changed.

Create containers without building using --no-build

In this step, we will explore the docker-compose create --no-build option. This flag is useful when your docker-compose.yml file includes services that are built from a Dockerfile (using the build instruction), but you only want to create the containers without rebuilding the images. Since our current docker-compose.yml file only uses a pre-built image (nginx:latest), the --no-build flag won't have a visible effect on the image building process in this specific case. However, it's important to understand its purpose for future use with services that require building.

First, ensure you are in the ~/project directory.

cd ~/project

Now, let's run the docker-compose create command with the --no-build flag. Since the container project_web_1 already exists from the previous steps, we will also use the --force-recreate flag to ensure a new container is created.

docker-compose create --no-build --force-recreate

In a scenario where your docker-compose.yml had a service defined with a build instruction, running this command would create the container using an existing image (if available) or pull the image, but it would skip the image building process defined in the Dockerfile. In our current case, since we are using a pre-built image, the output will be similar to the previous step, indicating the removal of the old container and creation of a new one.

Let's verify the container status again.

docker ps -a

You should still see the project_web_1 container in the Created state. The --no-build flag primarily affects services that are built from a Dockerfile, preventing the build process during container creation.

Create containers and pull images with --pull always

In this step, we will use the docker-compose create --pull always command. The --pull always flag instructs Docker Compose to always pull the latest version of the image before creating the container, even if a local copy of the image exists. This is useful to ensure you are always using the most up-to-date image for your services.

First, make sure you are in the ~/project directory.

cd ~/project

Now, run the docker-compose create command with the --pull always flag. Since the container project_web_1 already exists, we will also use the --force-recreate flag to create a new container based on the potentially updated image.

docker-compose create --pull always --force-recreate

When you run this command, Docker Compose will first check for updates to the nginx:latest image on Docker Hub. If a newer version is available, it will pull it. Then, it will remove the existing project_web_1 container and create a new one using the pulled image. You should see output indicating the pull process (if a new image is available), followed by the removal and creation of the container.

Let's verify the container status one last time.

docker ps -a

You will still see the project_web_1 container in the Created state. The key difference here is that Docker Compose actively checked for and potentially pulled a newer image before creating this container instance.

The --pull always flag is important for keeping your service deployments up-to-date with the latest image versions, ensuring you benefit from the latest features, bug fixes, and security updates.

Summary

In this lab, we learned how to use the docker compose create command to build and create containers based on a docker-compose.yml file. We began by preparing a simple docker-compose.yml file defining an Nginx service, which involved manually installing Docker Compose in the LabEx environment by downloading and making the binary executable. We then verified the installation.

Following the preparation, we explored various options of the docker compose create command. We learned how to create containers using the basic command, how to force recreation of existing containers with the --force-recreate flag, how to create containers without rebuilding images using the --no-build flag, and how to ensure the latest images are pulled before creation with the --pull always flag. These steps demonstrated the flexibility and control offered by docker compose create in managing container lifecycles.