Docker Basics for Web Developers

DevOps ~ 8 minutes

# Welcome to Docker

Hello and welcome! Have you ever heard "it works on my computer, but not on the server"? Docker was made to fix this problem. In this tutorial, we will learn the basics of Docker in a calm and simple way.

Docker puts your app and everything it needs into one box. This box is called a container. The container runs the same way on your laptop, on your friend's laptop, and on a server.

Words you should know

  • Image: a read-only template, like a recipe for your app
  • Container: a running copy of an image
  • Dockerfile: a text file with the steps to build an image
  • Registry: a place to store images, like Docker Hub

Do not worry about the new words. We will use each one in practice. Let us begin!

Check the Docker version
Code Copied to Clipboard

# Step 1: Install and check Docker

First, please install Docker Desktop from the official guide at docs.docker.com/get-docker. It works on Windows, macOS and Linux. After the installation, please open Docker Desktop and wait until it says that Docker is running.

Then run the command above in your terminal. You should see a version number. If you see it, Docker is ready. Now, let us run our first container.

Run your first container
Code Copied to Clipboard

What just happened?

Docker did not find the hello-world image on your computer. So it downloaded the image from Docker Hub, created a container, and ran it. The container printed a friendly message and then stopped.

This small test proves that the whole system works: the Docker engine, the internet download, and the container run. Great start!

# Step 2: Prepare a small Node.js app

Now, let us put a real app in a container. Please create a new folder named docker-demo. Inside, make a file server.js that starts a simple web server on port 3000, and a package.json file with a start script.

Next, we write a Dockerfile. The name has no extension, and the D is a capital letter. Let us write it in two small parts. The first part chooses the base image and prepares the packages.

Dockerfile (part 1)
Code Copied to Clipboard

# Reading the first part

Let us read each line slowly. FROM picks the starting image. Here we use Node.js 20 on Alpine Linux, which is very small. WORKDIR sets the folder inside the container. COPY brings your package files into the container. RUN runs a command while building the image.

We copy package.json first and install packages before we copy the rest of the code. Docker saves each step as a layer. This way, if only your code changes, Docker does not install the packages again. This makes the next builds much faster.

Dockerfile (part 2)
Code Copied to Clipboard

# Reading the second part

The second part copies all your project files with COPY . .. The EXPOSE line tells people that the app uses port 3000. Finally, CMD is the command that runs when the container starts.

We should also create a file named .dockerignore. Write node_modules inside it. This stops Docker from copying your local packages into the image. For more details, you can read the Dockerfile reference.

Now, we are ready to build the image.

Build the image
Code Copied to Clipboard
Do not forget the dot

Please look at the end of the command above. There is a dot (.). It tells Docker to use the current folder as the build context. If you forget it, Docker shows an error.

The -t docker-demo part gives a tag, which is a name, to your image. Please run the command from the same folder where your Dockerfile is saved.

Run the container
Code Copied to Clipboard

# Step 3: Run your app

Great! Let us understand this command part by part.

Meaning of each flag

  • -d runs the container in the background
  • -p 3000:3000 connects port 3000 of your computer to port 3000 of the container
  • --name web-demo gives the container an easy name
  • docker-demo is the image we built

Now, please open http://localhost:3000 in your browser. You should see your app. Wonderful!

See running containers
Code Copied to Clipboard

# Check your containers

The command above lists all running containers. You will see the container ID, the image name, the ports and the name web-demo. If you want to see stopped containers too, please add the -a flag.

Sometimes your app does not work as you expect. Do not worry. Docker lets you read the output of the container. This is the best way to find errors.

Read container logs
Code Copied to Clipboard

Handy tip for logs

Add the -f flag, like docker logs -f web-demo, to follow the logs live. Press Ctrl + C to stop watching. The container keeps running. This is very useful when you test a new feature.

Stop the container
Code Copied to Clipboard

Stop and clean up

When you finish your work, stop the container with the command above. A stopped container still exists on your disk. If you want to remove it fully, run docker rm web-demo. To remove the image, run docker rmi docker-demo.

Cleaning old containers and images is a good habit, because they use disk space. Now, let us learn one more very useful tool, called Docker Compose.

# Step 4: Docker Compose

Real web apps often need more than one service. For example, you may need a web app and a database. Typing long docker run commands for each one is tiring. Docker Compose solves this. You describe all services in one file named compose.yaml, and start everything with one command.

Let us write a file with a Node.js app and a MySQL database. We will write it in two parts. Here is the first part, for the app.

compose.yaml (part 1)
Code Copied to Clipboard

# The database service

In the first part, build: . tells Compose to build the image from our Dockerfile. The value DB_HOST: db is important. Inside Compose, each service can reach other services by their service name. So our app finds the database with the name db.

Now, we add the database service below the app service. Please keep the same indentation, because YAML is strict about spaces.

compose.yaml (part 2)
Code Copied to Clipboard

# Why we use a volume

Containers forget their data when they are removed. A volume is a safe storage place managed by Docker. We mapped db-data to the MySQL data folder, so your database stays even if you recreate the container.

Now, let us start both services together with one command.

Start all services
Code Copied to Clipboard

# Compose is running

Docker builds the image, downloads MySQL, creates a network and starts both containers. You can check them with docker compose ps. When you are done for the day, you can stop and remove everything with the next command.

Stop all services
Code Copied to Clipboard
Protect your secrets

In this tutorial, we wrote the password secret directly in the file. This is fine for learning, but it is not safe for a real project.

Safer habits

  • Keep passwords in a .env file and add it to .gitignore
  • Use strong passwords on servers
  • Never put real secrets inside an image, because others can read them

The command docker compose down keeps your volumes. If you add -v at the end, it deletes the volumes too, and your database data will be lost. So please be careful.

# Summary and next steps

Very good work! You now know the basics of Docker. Let us remember the main points.

What we learned

  • An image is a template, and a container is a running copy of it
  • A Dockerfile describes how to build an image
  • docker build, docker run, docker ps and docker logs are your daily commands
  • Docker Compose runs many services with one file and one command
  • Volumes keep your data safe

Where to go next

  • Read the official guide at docs.docker.com
  • Learn about multi-stage builds to make smaller images
  • Try running a PHP or Laravel app in Docker

Please practice with a small project of your own. Step by step, you will become confident. See you in the next tutorial!