- Create a Debian container
- New Dockerfile
- FROM debian
- Build image
- Run image
- Run cat /etc/issue
- docker build . -t [IMAGE_NAME]
- docker run -it [IMAGE_NAME] bash
Solution
FROM debian
docker build . -t debian_image
docker run -it debian_image bash
cat /etc/issue
- Create a Debian container (Dockerfile bluh bluh)
- Create a folder “html”
- Create inside folder an index.html file
- With some text
- Mount host folder ./html to /html folder in container
- "--mount type=bind,source="$(pwd)/html",target=/html"
Solution
FROM debian
mkdir html
touch html/index.html
echo "Hello SocialNerds!" >> html/index.html
docker build . -t debian_image
docker run -it --mount type=bind,source="$(pwd)/html",target=/html debian_image bash
cat /html/index.html
You should see "Hello SocialNerds!"
- Create a Debian container (Dockerfile bluh bluh)
- named debian_1
- Create a volume named “html”
- Mount volume “html” to /html folder in container
- Add a new file named “index.html” in /html folder
- Create a new Debian container named debian_2
- Mount volume “html” to /html folder in container
- docker volume create [VOLUME_NAME]
- --mount source=[VOLUME_NAME],target=/html
- --name [CONTAINER_NAME]
Solution
FROM debian
docker volume create html
docker build . -t debian_image
docker run -it --name debian_1 --mount source=html,target=/html debian_image bash
touch /html/index.html
exit
docker run -it --name debian_2 --mount source=html,target=/html debian_image bash
ls -la /html/index.html
- Run ./clearall
- Go to 4_Network folder (cd 4_Network)
- Run ./setall
- Run "docker ps" to check everything is there
- Create a network named apache_tomcat_mysql
- Connect all containers to it
- Connect mysql container with "superdb" alias
- docker network connect [NETWORK_NAME] [CONTAINER_NAME]
- docker network connect --alias [ALIAS] [NETWORK_NAME] [CONTAINER_NAME]
Solution
docker network create apache_tomcat_mysql
docker network connect apache_tomcat_mysql apache
docker network connect apache_tomcat_mysql tomcat
docker network connect --alias superdb apache_tomcat_mysql mysql
- Run ./clearall
- Run a MySQL 5.7 container and pass environment variables for
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE (Creates a database by default)
- MYSQL_USER (Creates a user for the default database)
- MYSQL_PASSWORD (Sets a password for this user)
- "-e MYSQL_ROOT_PASSWORD=[MYSQL_ROOT_PASSWORD]"
Solution
docker run -d -e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=my_default_database \
-e MYSQL_USER=my_default_user \
-e MYSQL_PASSWORD=password \
mysql:5.7
- Run ./clearall
- Create a new folder and cd to this folder
- Run a MySQL 5.7 container and pass environment variables
through an .env file for
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE (Creates a database by default)
- MYSQL_USER (Creates a user for the default database)
- MYSQL_PASSWORD (Sets a password for this user)
- "--env-file .env"
Solution
touch .env
echo "MYSQL_ROOT_PASSWORD=root\n\
MYSQL_DATABASE=my_default_database\n\
MYSQL_USER=my_default_user\n\
MYSQL_PASSWORD=password\n" \
> .env
docker run -d --env-file .env mysql:5.7
- Run ./clearall
- Create a new folder and cd to this folder
- Create Dockerfile and a "start" script
- In the start script add the command "tail -f /dev/null"
- Create a Dockerfile "FROM ubuntu"
- In the Dockerfile create a user named "serveruser"
- COPY the script inside the Docker image under /home/serveruser/
- Set the owner of the start script as "serveruser"
- Make the start script the default command
- Create user "RUN useradd -ms /bin/bash [USERNAME]"
- Add execute permission "RUN chmod +x [FILE]"
- Copy from host to image "COPY [HOST_SOURCE] [IMAGE_DESTINATION]"
- Script annotation "#!/bin/bash"
- Default command "CMD [COMMAND]"
Solution
FROM ubuntu
RUN useradd -ms /bin/bash serveruser
COPY ./start /home/serveruser/start
RUN chmod +x /home/serveruser/start
RUN chown serveruser:serveruser /home/serveruser/start
CMD ["/home/serveruser/start"]
#!/bin/bash
tail -f /dev/null
docker build . -t ubuntu_image
docker run -d ubuntu_image
- Run ./clearall
- Create a Dockerfile "FROM ubuntu"
- In the Dockerfile create a user named "serveruser"
- Build the image UID=512 for "serveruser"
- Create user "RUN useradd -ms /bin/bash -u [UID] [USERNAME]"
- Dockerfile syntax "ARG [ARGUMENT]"
- Command "--build-arg UID=[UID]"
Solution
FROM ubuntu
ARG UID
RUN useradd -ms /bin/bash -u $UID serveruser
docker build . --build-arg UID=512 -t ubuntu_image
- Run ./clearall
- cd to 9_Docker_Entrypoint folder
- When docker-entrypoint.sh runs, create a new folder /home/serveruser/code
- This new folder must be owned by user "serveruser"
- The name of the folder must pass as env variable E.g. "CODE_FOLDER"
- Create folder: "mkdir /home/serveruser/[FOLDER_NAME]"
- Change ownsership "chown -R [USERNAME]:[GROUP] /home/serveruser/[FOLDER_NAME]"
Solution
###
mkdir /home/serveruser/$CODE_FOLDER
chown -R serveruser:serveruser /home/serveruser/$CODE_FOLDER
###
docker build . -t ubuntu_image
docker run -d --name ubuntu_container -e UID=512 -e CODE_FOLDER=code ubuntu_image
- Run ./clearall
- cd to 10_Docker_Compose folder
- Add a new service named app_http_2
- Add to the same network as app_http
- No exposed ports
- Same command
- Same volume
- What hints? It is just copy paste :)
- Build images: docker-compose build
- Run images: docker-compose up -d
- Stop and Remove: docker-compose down
Solution
http_2:
image: ubuntu:18.04
command: ["tail", "-f", "/dev/null"]
container_name: app_http_2
volumes:
- ./data:/data
networks:
app_network:
- Run ./clearall
- cd to 11_Docker_Compose_env folder
- Pass an environment variable to fpm service named APP_DEBUG
- The value of the variable should be "TRUE"
- Don't forget to add the variable in the .env file
- Run ./clearall
- cd to 12_Docker_Compose_args folder
- Pass an argument to fpm service named UID
- The value of the variable should be the user id of your user on your machine
- Type "id" to get user id
- Google "docker compose arguments"
Solution
fpm:
build:
context: ./fpm
args:
UID: ${UID}
container_name: fpm
.....
UID=1234
- Run ./clearall
- cd to 13_Docker_Compose_networks folder
- Set a network for http and fpm named app_public_network
- Set a network for fpm and db named app_private_network
Solution
version: '3.8'
services:
http:
build:
context: ./http
args:
UID: ${UID}
container_name: http
restart: always
volumes:
- ./code:/home/serveruser/code
ports:
- 8080:80
networks:
app_public_network:
fpm:
build:
context: ./fpm
args:
UID: ${UID}
container_name: fpm
restart: always
volumes:
- ./code:/home/serveruser/code
networks:
app_private_network:
app_private_network:
db:
build:
context: ./db
container_name: db
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- dbdata:/var/lib/mysql
networks:
app_private_network:
networks:
app_public_network:
app_private_network:
volumes:
dbdata:
- Run ./clearall
- cd to 14_Docker_Compose_override folder
- Create a new file docker-compose-dev.yml
- Add to docker-compose-dev.yml only the db service configuration
- Remove the db service configuration from docker-compose.yml
Solution
version: '3.8'
services:
db:
build:
context: ./db
container_name: db
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- dbdata:/var/lib/mysql
networks:
app_network:
volumes:
dbdata: