How to install docker and get your first container running

Step 1 #

Prepare your Ubuntu for Docker:

$ apt-get update
$ apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release

 

Step 2 #

Add official Docker GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

 

Step 3 #

Add official Docker repository:

$ echo “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

 

Step 4 #

Install Docker engine:

$ apt-get update
$ apt-get install -y docker-ce docker-ce-cli containerd.io

 

Step 5 #

Type docker ps to check if the docker engine is running:

$ docker ps

docker-ps.png

Step 6 #

Prepare your first dockerfile with Ubuntu 20.04 (with Apache and PHP):

$ mkdir Ubuntu
$ cd Ubuntu
$ nano Dockerfile

 

Step 7 #

Now paste below content to Dockerfile:

FROM ubuntu:bionic

RUN \
DEBIAN_FRONTEND=”noninteractive” apt update && apt -y upgrade && apt -y install tzdata software-properties-common && add-apt-repository ppa:ondrej/php -y && apt-get update && \
# Apache
apt -y install apache2 && \
# PHP
apt -y install libapache2-mod-php7.4 php7.4-zip php7.4-xmlrpc php7.4-xml php7.4-soap php7.4-mysql php7.4-gd php7.4-intl php7.4-curl php7.4-bz2 php7.4-bcmath php7.4-mbstring && \
# MOD rewrite
a2enmod rewrite

EXPOSE 80
CMD [“/usr/sbin/apache2ctl”, “-DFOREGROUND”]

As you can see, Dockerfile contains PHP7.4 from ondrej repository. Save your file and type:

$ docker build -t apache .

Wait a while until Docker build the image.

Step 8 #

Now run your Docker image:

$ docker run -d -p 80:80 apache

 

Step 9 #

Check if your docker image start running:

$ docker ps

 

docker-ps2.png

Step 10 #

You can see the running container, now check if the Apache is running:

$ curl http://localhost

You should get Apache welcome website.

Step 11 #

If you want to enter to the container, please copy the container ID and type:

$ docker exec -it {CONTAINER_ID} bash