Commit a container as an image

Once you have made changes to a container you can save it as a new image.

docker commit <containerid> jpcloud/wget:1.0

Then this new image can be run again

docker run -it jpcloud/wget:1.0 /bin/bash

Inefficient dockerfile
Create file with below contents;

# Comment
FROM centos:7
RUN yum install wget -y
RUN yum install vim -y

Run file with;

docker build -t jpcloud/testlong:1.0 .

Efficient dockerfile
Aggregated RUN intructions;

# Comment
FROM centos:7
RUN yum install wget -y && yum install vim -y
[or you can do this]
RUN yum install -y wget \

Add a command to run on an image

# Comment
FROM centos:7
RUN yum install wget -y && yum install vim -y
CMD ["ping", "localhost", "-c", "20"]

Override the default command in an image

docker run -t jpcloud/test:1.1 echo "wally"

You can also supply parameters to an image during startup. In this example, the image is built with and “Entrypoint”

# Comment
FROM centos:7
RUN yum install wget -y && yum install vim -y

This image can then be run with parameters

docker run -t jpcloud/test:1.2 localhost -c 10
Stopping/Starting Containers
docker ps -a                -[list all containers]
docker ps                   -[list running containers]
docker stop <containerid>   -[stop by continerid]
docker start <containerid>  -[start by containerid]
docker start <image name>   -[start by image name]
Container Shell Access

Used to connect to a running container.

docker exec -it <containerid> /bin/bash
Container Deletion

Container must be stopped before deleting

docker rm <containerid>
Delete Local Images

Confirm image and tag first

docker rmi jpcloud/test:1.2
NOTE: you can also use <imageid>
Push An Image To Docker Hub

You might want to push a local image which doesn't have the same repo name as your docker hub repo name. If you push an image it will look for the matching repo name and fail if it doesn't exist. In this case you would need to tag the local image to one of your valid repo names before trying to push.

docker tag test/test:1.2 jpcloud/test:1.2
docker push jpcloud/test:1.2
Working With Volumes

Volumes are simply a path which is mounted onto the container filesystem.

  1. Share data between containers
  2. See data written in the container directly from the host


  • Let docker manage the host location of the volume

In this case the volume is stored on the host in /var/lib/docker/volumes/

docker run -d -P -V /test nginx

NOTE: you can also do this in the dockerfile

VOLUME /test
  • Specify the host path to mount on the container

In this case /var/log on the host is mount to /www/log in the container

docker run -d -P -V /var/log:/www/log nginx

Ports can be mapped to the host during container startup

docker run -d -p 8080:80 nginx

You can also use “-P” instead of “-p” but this will auto map any container ports referenced in the docker file by the “EXPOSE” syntax. Problem with this is working out what the matching host port is !!!