Docker Basics

The below docker commands should give you a basic understanding of how docker works.  This is obviously not a comprehensive lesson, but should give you enough information to take off on your own.

Create an account

Run a basic container

Run in detached mode on internal port 80, external port 5001 using the httpd:2.4 image on docker hub, and name the container itsmetommy-httpd.

docker run -d --name itsmetommy-httpd -p 5001:80 httpd:2.4
  • -name <container_name>
  • -d == detached mode
  • -p <external_port>:<internal_port>
  • httpd:2.4 == <image_name>:<version>

View your URL


View your images

docker images | grep httpd

View your running docker containers

docker ps

View your running and stopped docker containers

docker ps -a

Run centos in interactive and tty mode

docker run -it --name itsmetommy-centos centos

Then exit out of the container.

  • -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container
  • exit out of the container

View your running docker containers

docker ps

View your running and stopped docker containers

Notice that itsmetommy-centos is stopped. This is due to you not running in detached mode.

docker ps -a

Start itsmetommy/centos back up

docker start itsmetommy-centos

View your running docker containers

Notice that itsmetommy/centos is now running.

docker ps

Lets connect to itsmetommy/centos

docker attach itsmetommy-centos

If you type ‘exit’ you will by design stop the container.

This is where pressing CTRL P + Q comes in handy.


You can view that itsmetommy-centos is still running.

docker ps

Stop the docker containers

docker stop itsmetommy-httpd;docker stop itsmetommy-centos

View your running and stopped docker containers

docker ps -a

Remove the docker container

docker rm itsmetommy-httpd;docker rm itsmetommy-centos

Create a project directory

Dropbox Location — docker-examples/learn-docker

mkdir learn-docker;cd learn-docker

Create a public-html directory with index.html

mkdir public-html;echo "Hello World" > public-html/index.html

Create Dockerfile

What if we want to record our changes and build our own image? Dockerfiles to the rescue!

This will download the httpd image from docker hub and copy the contents from public-html into /usr/local/apache2/htdocs.

vi Dockerfile
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/


This will build a new image called itsmetommy/learn-docker based on the Dockerfile.

docker build -t itsmetommy/learn-docker .
  • -t tag


Make sure you are pointing to localhost and logged into docker hub.

eval $(docker-machine env --unset) # unset env to localhost

env | grep DOCKER # view environment

If you haven’t already, login using your Docker Hub account.

docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to to create one.
Username: itsmetommy
Login Succeeded

Now push your new image to your docker hub.

docker push itsmetommy/learn-docker


Volumes give a running container the ability to see files on the host machines file system. They DO NOT change the image.

If you ever wanted to make any changes to lets say index.html, it would be a pain to have to re-build the image every time you test a change. This is where mounting your local volume within the container comes into play.

Lets run the container again except with a volume mount.

Note: You must put the full path (not relative).

docker run -d -p 5001:80 -v /Users/$USER/learn-docker/public-html:/usr/local/apache2/htdocs/ --name itsmetommy-learn-docker itsmetommy/learn-docker

IMPORTANT: If you update any files, you will need to re-build the image to get an updated copy of the image.



Now update your local index.html file.

echo "Hello $USER" > public-html/index.html

http://localhost:5001 # refresh browser

Awesome right? 🙂

Clean up — Stop/remove/delete the container

docker stop itsmetommy-learn-docker;docker rm itsmetommy-learn-docker;docker rmi itsmetommy/learn-docker

Doing a bunch of docker run commands can be a huge pain.

That’s where docker-compose saves the day.


Create a learn-docker-compose folder and cd into it

mkdir learn-docker-compose; cd learn-docker-compose

Create the httpd and nginx folders and files

mkdir -p httpd/public-html nginx/public-html; echo "Hello HTTPD" > httpd/public-html/index.html; echo "Hello NGINX" > nginx/public-html/index.html

Create docker-compose.yml

vi docker-compose.yml
version: '3'

    build: ./httpd
    image: httpd
      - ./httpd/public-html:/usr/local/apache2/htdocs
      - 5001:80
    container_name: itsmetommy-httpd
    build: ./nginx
    image: nginx
      - ./nginx/public-html:/usr/share/nginx/html
      - 5002:80
    container_name: itsmetommy-nginx

Run docker-compose up

docker-compose up -d # detached mode

View your sites


Update index.html and refresh browsers

Update the httpd and nginx index.html file.

echo "Hello HTTPD 2.0" > httpd/public-html/index.html
echo "Hello NGINX 2.0" > nginx/public-html/index.html

http://localhost:5001 # refresh
http://localhost:5002 # refresh

Run docker-compose kill

docker-compose kill