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 –
|You can add a custom map from host to IP.|
|-a||You can use it to attach to the streams such as STDIN, STDOUT or STDERR.|
|-e||You can use this option to set environment variables.|
|You can expose one or more ports using the expose option.|
|-i||You can use the STDIN even if not attached.|
|You can assign a name to the container.|
|–volume||-v||You can mount volumes using the option.|
|–rm||As soon as you exit the container, it will remove it.|
|–publish||-p||You can use this option to publish exposed ports.|
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.
- 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.
- 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.
- Next, you can invoke the container start command to start the created container. Your container is now in the started state.
- 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
Let’s list all the containers to verify.
$ docker ps -a
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
You can see that we now have access to the bash of the container. Let’s check the status of this 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
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.