
It’s undeniable that Docker (and containerization in general) has been gaining popularity in recent years. If you work in IT, you’ve most likely heard people talking about it. So, today seems like the perfect chance for me to show you some basics.
Some experience with Linux is required as I won’t go that deep into details.
Requirements:
- Docker Hub account
- Host running Linux
Here’s a brief description of what we’ll be doing in this short tutorial:
- Installing Docker on a Linux machine
- Creating our own Docker image
- Setting up Apache and creating a website from that image
Let’s start by installing Docker. To achieve this run
In Manjaro: sudo pacman -S docker
In CentOS/RHEL: sudo yum -y install docker
Once that’s done, we need to add our user (who has sudo privileges) to the group docker
sudo usermod -aG docker your_user
Start the docker service by executing
sudo systemctl start docker
Check that the service is running and we should be good to go. We’ll now pull the latest official CentOS image from Docker Hub
docker pull centos:latest
After downloading the image, we can check that it’s actually present by running
docker images
To run a container using this image
docker run -it --name container_test centos
A short explanation of what the command we just ran is doing
- docker run: Starts our container
- -it: Starts an interactive TTY
- –name: We pass a name to our container, in this case “container_test”
- centos: It’s the name of the image we previously downloaded
After you’re done playing around with your container, type exit to quit. In the next few steps, we’ll create our own image, using ‘centos’ as our base image, but we’ll also install Apache as our webserver and create an index.html file in the default DirectoryRoot of our container.
In your local machine, start by creating a test directory, and inside, create an index.html (this will serve our webserver) and another file called Dockerfile (we’ll jump into the details soon)
mkdir docker-test
cd docker-test
touch index.html <- Content of the file can be anything
touch Dockerfile
Open up Dockerfile and paste the following content
FROM centos:latest
RUN yum -y update
RUN yum -y install httpd
COPY index.html /var/www/html
ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]
EXPOSE 80
Basically what this is doing is:
- FROM: Selects our base image. If the chosen image wasn’t previously downloaded, it’ll do it at the time we build it. In our case we are downloading the latest centos version available.
- RUN: Executes this/these command inside of our container.
- COPY: Copies the files in the current directory to a specific path inside our container. In this case we are copying the local index.html file created to /var/www/html inside of our container.
- ENTRYPOINT: Commands that will be ran first. In our case, this will keep Apache running in the background.
- EXPOSE: Port number we want to expose for our container.
To build the image we just configured, run
docker build -t docker_hub_user/apache-test .
Where ‘docker_hub_user’ is well…your Docker Hub user, and ‘apache-test’ is our image reference name.
That’s it! We now have a custom image created. Let’s run it in a container.
docker run -d -p 1234:80 docker_hub_user/apache-test
The flag -d runs our container in detached mode, -p allows us to choose a random port/do port forwarding to a port in our container. Now, head to your browser and type localhost:1234. You should see the content of your index.html file.
To delete a container, check the active running containers with ‘docker ps’ and delete them with ‘docker rm -f container_id’ , where container ID is the ID you see in the output of your ps command.
That’s the very end of our guide, hope you enjoyed it!