Create Your First Docker Container

Create Your First Docker Container

Category : How-to

Get Social!

docker-logoDocker is probably one of the easiest environments to create a virtualised instance based on a number of flavours of operating systems. Rather that having to install an operating system yourself, you can download one of the many guests templates or ‘images’ available directly from the Docker community.

See my blog post on installing Docker on Ubuntu 14.04 if you don’t currently have Docker installed.

There are a number of commands which are required to manage Docker containers and images. First off, let’s see if we have any images in our local Docker library.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

The docker images command lists the available local images which you can use to create a Docker container. The above output does not show any local images so lets download one from the central Docker repository.

We must choose which image to download and use to create our first Docker container. There are literally thousands of images available on the central repository and all can be downloaded through the docker command. Let’s use the search command to find an image to download.

$ docker search ubuntu

This will display a huge list of all the images available containing the word ubuntu. As you can imagine, there will be hundreds because not only are base OS images available, but customised images containing specific applications or set ups.

Let’s download the basic ubuntu 14.04 image:

$ docker pull ubuntu:14.04
Pulling repository ubuntu
ad892dd21d60: Download complete
511136ea3c5a: Download complete
e465fff03bce: Download complete
23f361102fae: Download complete
9db365ecbcbb: Download complete

You can check this has downloaded the image to your local store with the above docker images command. We will also need to make a note of the image ID so that we can use it to create a container from it.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               ad892dd21d60        11 days ago         275.5 MB

The next step is to create a container and make the required changes. Creating a container is Docker is done with the run command followed by, amongst other things, a command to run within the container. We are going to create a new container and use a bash session to customise the container before saving it as a new image for use in the future.

Create the Docker container with the run command and specify the bash shell to be executed on completion. This will leave us with a bash session which we can use the customise the image. Replace the ad892dd21d60 ID with the ID of the image we downloaded in the previous step.

$ docker run -i -t ad892dd21d60  /bin/bash
root@3a09b2588478:/#

You now have an active shell on the container which has been created with the id 3a09b2588478. Type exit to end the session in your guest container and the container will be stopped and kept available on your Docker system.

Run the ps Docker command to see what containers are known to your Docker system.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f4b0d7285fec        ubuntu:14.04        /bin/bash           8 minutes ago       Exit 0                                  hungry_thompson
8ae64c0faa34        ubuntu:14.04        /bin/bash           10 minutes ago      Exit 0                                  jovial_hawking
3a09b2588478        ubuntu:14.04        /bin/bash           14 minutes ago      Exit 130                                kickass_lovelace

The above output shows 3 containers which are available in my Docker system with the container ID on the left. We can re-enter one of these containers to make our changes, but first we need to start it. I’m going to use container ID 3a09b2588478 for the rest of this example but yours will be a different ID.

$ docker start 3a09b2588478

We can now attach to the container to create a shell where we can make our modifications.

$ docker attach 3a09b2588478

You now have a shell running on the container which you can use to make your changes to the container. Let’s keep it simple and just run an upgrade with apt-get and then exit. In the real world, you might install an application, or define your configuration such as LDAP SSH login.

$ apt-get update
$ apt-get upgrade
$ exit

The last step in our example is to save the container as a new image which can be used to create future Docker containers. You’ll need to specify the container ID as well as the name of the image to use. You can specify a new image name or overwrite the existing image name.

$ docker commit 3a09b2588478 ubuntu:14.04
b2391f1efa6db419fad0271efc591be11d0a6d7f645c17487ef3d06ec54c6489

 

And that’s all there is to it! You have created a new Docker container, from one of the images available from Docker, made some changes and saved it locally for future use. Of cause, there are plenty more ways to use Docker, but I hope this has been useful for getting a basic understanding of how Docker works.

Next steps: See my post on using a Dockerfile to automate Docker image creation.

Quick Poll

Are you using Docker.io?


17 Comments

Spirit

11-Sep-2015 at 11:55 pm

Well written! Very helpful for beginners to get the idea in a minute. You mentioned that ‘docker run’ is to create containers, help says that ‘docker create’ is for creating new containers which is confusing. When I do docker create , it outputs a long 64 char ID and adds a container in the docker ps -a list. Explanation of what that ID is and how to start it would be great. Thanks.

Mister Zaug

29-Sep-2015 at 8:23 pm

OK, now what? Shouldn’t I have a new image as a result? What does that long string the commit spits out represent? b2391f1efa6db419fad0271efc591be11d0a6d7f645c17487ef3d06ec54c6489 is a new image name? If so, it does not show up when I type ‘docker ps -a’

Eduardo Medina

5-Dec-2015 at 5:10 pm

Thank you very much, your tutorial is very practical, so it’s very good for technicians like me who never used Docker.

Francis Kim

27-Dec-2015 at 12:25 am

Great tips, thanks James!

anup

10-Mar-2016 at 7:22 am

Good blog thanks a lot….

Dong Banowitz van Esaamuyéz

3-Apr-2016 at 11:38 pm

In this article is nowhere described how a container is created. Instead it describes how to load and execute code from some internet site on your computer. There is no actual description of how to create a container.
Unfortunately the internet is full of these unprecise descriptions of “experts” that will lead many people into very bad situations. Again: you are executing code on your computer that you do not know anything about. There is no reason to do this and it subverts any idea of substantial security.

    Ben

    11-Aug-2016 at 9:04 am

    Hi

    While mostly you are correct about this article not teaching creation of actual image, it is still useful for total beginners who never ran docker commands at all..

    Surely enough that people will understand that docker ps -a command wont create an image but will show whats available…

    Cheers

Lokendra Singh

7-Apr-2016 at 9:53 am

Hello There !!
I am new to Docker, and now I want install and use docker in my environment . Can anyone suggest me how can I import my running applications to docker. Moreover I need to create multiple containers. So can I use multiple commits as container ?

Mary

16-Sep-2016 at 4:43 pm

Thanks for the post. A really quick and successful setup for me.

June

21-Jan-2018 at 3:39 pm

Thanks a lot for your writing. This is exactly what I am looking for.

Anurag

28-Jan-2018 at 9:38 am

Thank you for such an informative column.
Useful for a beginner like me.

Antony Sebastian

8-Mar-2018 at 6:47 am

Well written. Awesome….

Zeeshan

27-Jun-2018 at 3:41 pm

Thanks,
Well explained.

Samuel

1-Jul-2018 at 7:10 am

Nice job, save me much time.Thanks

Joseph Spenner

11-Jun-2019 at 12:26 am

Very nice simple for people wanting to get started. Something else you might want to mention is how to actually save your own image to a local file. I found this useful if I didn’t want to mess with the registry, or if I have something sensitive and want to save it. You can create/use your own registry, or you can save it locally as a file as follows:

$ docker export CONTAINDER_ID > myOwnImage.tar

Then to restore that:

$ docker import myOwnImage.tar

To save space, you can gzip/bzip2 the image. The import will respect a gzip/bzip2 image as well.

Nikhil

2-Jan-2020 at 2:08 am

Will docker run on RHEL6.10 with kernel version 2.6.32-754.17.1.el6.x86_64

Ajeya

5-Jul-2020 at 1:56 am

Thanks.
Easy to follow.
Read it and got it done.

Leave a Reply