Docker container create | How to create Docker containers?


You can use the Docker container create command to start a container from an image. However, the container create command only creates a writable container layer over the image. Simply put, it creates a container instance but does not start a container. The container create command is almost similar to “docker run -d” with the exception that it never starts a container. You can then use the “docker start” command to start the container whenever you want.

This command is useful when you just want to set up the configuration of the container beforehand so that it is ready when you want to start the container. On running the container create command, the status of the container is created.

In this article, we will discuss how to create Docker container from image using the container create command. We will see various examples using different options as well. Before you move ahead, check out our complete and free Docker Tutorial.

Docker Container Create from an Image

Let’s check out the syntax of the container create command.

$ docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]

You can also omit the container keyword from the above command and still get the same result.

$ docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

You can use certain important options along with this command. These are –

NameShorthandDescription
--add-hostYou can add a custom map from host to IP.
--attach-aYou can use it to attach to the streams such as STDIN, STDOUT or STDERR.
--env-eYou can use this option to set environment variables.
--exposeYou can expose one or more ports using the expose option.
--interactive-iYou can use the STDIN even if not attached.
--nameYou can assign a name to the container.
–volume-vYou can mount volumes using the option.
–rmAs soon as you exit the container, it will remove it.
–publish-pYou can use this option to publish exposed ports.
Options to use with container create command

How to create and Start a container?

As already discussed above, you can use the container create command to just create container instances. It does not start the container. To better understand the entire concept, you can check out the article on Docker Container Lifecycle. In short, the following are essential states.

  1. To start with, you use the Docker build command to build images from Dockerfile. Or you can use the Docker pull command to directly pull images from Docker registries.
  2. Now, you have your image ready. You can now use the Docker create command to create containers associated with this image. Your container is now in the created state.
  3. Next, you can invoke the container start command to start the created container. Your container is now in the started state.
  4. You can do all the above steps directly using the Docker run command as well. This command pulls an image from Docker registry, creates a container, starts it, and you can have access to the bash of the container.

Let’s see how to create a container. We will create a container associated with the Ubuntu image in interactive mode.

$ docker create -it --name=myubuntu ubuntu
Create Docker Container from an Image
Create Docker Container from an Image

Let’s list all the containers to verify.

$ docker ps -a
Container is in created state
Container is in created state

You can see that the container is now in created state.

Let’s use the Docker start command to start this container and access it’s bash.

$ docker start -a -i myubuntu
Start a Docker Container
Start a Docker Container

You can see that we now have access to the bash of the container. Let’s check the status of this container.

Status of the container
Status of the container

The container is now up and running. Instead of doing all these steps, you can directly use the Docker run command to pull, create, start, and get access to a container’s bash.

$ docker run -it --name=myubuntucopy ubuntu bash
Docker run command
Docker run command

Final Thoughts!

You can use the Docker container create command to create container from images. This will create a writable container layer over the image. However, the container is now only in the created state. To get access to the container, you can start the container using the Docker container start command. You can then have access to the bash of the container. Also, you can directly use the Docker run command to jump through all these steps and get access to the container’s bash directly.

If you have any queries or suggestions, please mention them in the comment box and we will have our experts get back to you as soon as possible. Also, check out our complete and free Docker Tutorial.

Helpful Articles –

  1. Docker Container Lifecycle
  2. How to Build Docker Images?
  3. How to start a Docker container?

Leave a Reply