Skip to content

Instantly share code, notes, and snippets.

@saissemet
Last active March 4, 2021 10:08
Show Gist options
  • Save saissemet/33955c7f6681f32d809cb4234e859c1e to your computer and use it in GitHub Desktop.
Save saissemet/33955c7f6681f32d809cb4234e859c1e to your computer and use it in GitHub Desktop.
## In this tutorial we are creating a simple Apache2 docker image. The main ideia is to create and push a docker image from scratch. ##
## Creating a local directory to store the configuration files ##
mkdir myapp
cd myapp
## Creating a "Dockerfile" with the following content ##
nano Dockerfile
FROM ubuntu:18.04
MAINTAINER saissemet
RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_LOG_DIR /var/log/apache2
RUN mkdir -p $APACHE_RUN_DIR
RUN mkdir -p $APACHE_LOCK_DIR
RUN mkdir -p $APACHE_LOG_DIR
COPY index.html /var/www/html
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
## Creating a "index.html" with the following content ##
nano index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Padre Messias II</title>
</head>
<body>
<h1>"Don't play, pray!" - Padre Messias II</h1>
</body>
</html>
## Update and upgrade the O.S.'s software ##
apt update && apt upgrade -y
## Installing docker ##
apt install docker.io
## Building the docker image. ##
## The name you choose in this step is'nt the one you will use to push your image ##
docker build -t [DOCKER_IMAGE_NAME] .
## Testing the image. ##
## The '-d' variable runs the container in the background. ##
## You can change the first port of the command. The second is the one the images uses. ##
docker run -d -p 80:80 [DOCKER_IMAGE_NAME]
## At this point, you should be able to open a browser page with your IP and see a html page ##
## Now we are moving on to push the image we just created. To start, you need to set up an account at the DockerHub (https://hub.docker.com/) ##
## After that, you can get back to the command line ##
## Login your DockerHub account ##
docker login
## Check you docker images and grab the image ID ##
docker images
## Tag your image. This tag will be used to push and pull your image. ##
docker tag [IMAGE_ID] [USERNAME]/[SOME_RANDOM_NAME]:[SOME_COOL_TAG]
## Push your image to the DockerHub repository ##
docker push [USERNAME]/[SOME_RANDOM_NAME]:[SOME_COOL_TAG]
## To test, go to some other computer and try this. ##
## Pull the docker image ##
docker pull [USERNAME]/[SOME_RANDOM_NAME]:[SOME_COOL_TAG]
## Run the docker image and check if it's working ##
## The '-d' variable runs the container in the background. ##
## You can change the first port of the command. The second is the one the images uses. ##
docker run -d -p 80:80 [USERNAME]/[SOME_RANDOM_NAME]:[SOME_COOL_TAG]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment