Code docker Linux

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

Login to docker hub

docker login

Search docker hub

docker search centos

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>
  • <image_name>:<version> (e.g. httpd:2.4)

View your URL


View your images

docker images | grep httpd

View running docker containers

docker ps

View running and stopped docker containers

docker ps -a

Run centos in interactive and tty mode

docker run -it --name itsmetommy-centos centos

Exit 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 stop the container
  • Pressing CTRL P + Q will exit and keep the container running

You can see 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 containers

docker rm itsmetommy-httpd;docker rm itsmetommy-centos

Create a project directory

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 a 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 within the httpd container.

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


Important: Create a repository on docker hub. My repository is named itsmetommy.

Now push your new image to your docker hub repository.

Important: Substitute itsmetommy with your repository name. 

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.

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

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

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

Docker-compose: 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

Docker-compose: Run docker-compose up

docker-compose up -d
  • -d detached mode

Docker-compose: View your sites


Docker-compose: 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

Docker-compose: List containers

docker-compose ps

Docker-compose: Run docker-compose stop

This stops the containers.

docker-compose stop

Docker-compose: Run docker-compose kill

If the containers won’t stop, you can issue a kill.

docker-compose kill

Docker-compose: Finally, remove the containers

docker-compose rm

By Tommy Elmesewdy

Data Janitor