Docker basic usage in 2021

 Hello,


so, if you've installed a Docker on your OS, there's an easy way to test it and understand how it is working.


1. Create a folder in any location, enter it.

2. Create an index.php file in the subfolder called 'src' with an example code like:

<?php
echo "Hey there!";

3. Save the file. 

4. Go back to the main folder.

5. Create a file with no extension, just the name: Dockerfile. 

6. Add the content: 

FROM php:8.0-apache
COPY src/ /var/www/html
EXPOSE 80

7. Save the file. The source for this file, comes from the Docker Hub, as well called the Docker store.

8. In the main folder of this project, you can run a command to build this project:

docker build -t Hello World .

Hello world is just any name for it. The dot at the very end, tells that the location is the current location of this project.

9. Now we can run this image/project with a command from the same folder location:

docker run -p 80:80 Hello World

If your 80 port is occupied, you can use some different one which is spare, like 8001. So, now code should be:

docker run -p 8001:80 Hello World

10. You can access your project now at the address in the web browser:

http://localhost:8001/

During the development, you can do more. You'd like to for sure see changes when you edit the code, yes?

If so, there's a solution to that called volumes.

We need to add '-v location from : location to which in our case is based on Linux, so /var/www/html

Here is a complete code with my ~/Documents/HelloWorld/src location, you can choose your own one, but has to point out to your 'src' folder in this image/project:

docker run -p 8001:80 -v ~/Documents/HelloWorld/src:/var/www/html Hello World

Image - this is a template for the container.

When you run an image, you run a container.

The next part in learning should be Docker Compose to connect containers in one project.

Additional commands in Docker:

docker-compose up -d   : this one is running an image in the background so that you can continue using your teminal.

docker-compose stop : to stop all of those running.

docker ps : this one list all the details, like container id, image, command, created, status, ports, names.





No comments:

Post a Comment