Docker – Basic commands

Create new Docker container

Start off with an existing image. For example ‘Ubuntu’ to start modifying. There are many others, find them here: https://hub.docker.com/

docker pull ubuntu

Then find the ID of your downloaded image:

docker images

In the list, look for the IMAGE ID of your downloaded image. In my case, for Ubuntu, it’s d355ed3537e9. Now, to create a container from this image, run:

docker run -it -p 5001:5001 d355ed3537e9 /bin/bash (-p for port forwarding, <host port>:<inside container port>

Now the container starts and you are connected to a shell within the container.

Now it’s possible to make any changes you want. For example:

apt-get update && apt-get upgrade

When finished setting up your container, it’s time to persist these changes to a new image. You can stop and rerun the same container and it will still have your changes, but to create a new container based on your container, we need an image. Persist you container to an image like this:

docker stop d355ed3537e9

docker commit d355ed3537e9 mynewimage

docker run -td mynewimage

To connect to the container after it is running use the exec command:

docker exec -it mycontainer bash