Environments are not the same. For example you develop on a Macbook and server is Ubuntu server. Docker runs on containers. Docker file builds a docker image, which contains all project code,installments of needed programs, complete application wrapped in an image, which is designed to sit on top of a container.
Create file called Dockerfile
Take a look at workspace/tutorial
project.
FROM
will take from the Python image for DockerHub.COPY
current directoryRUN
python specific stuff in requirements.txtCMD
Commands to run, first the python command and then any python filesFROM python:3-onbuild
COPY . /usr/src/app
RUN pip install -r requirements.txt
CMD ["python", "api.py"]
You can run services like:
docker run
These usually has several arguments like port numbers, which can become tedious that is why it is better to use a docker-compose.yml
file, like below. docker-compose
is a yaml file and spins up all the containers we need.
version: '3'
services:
# you name the services
product-service:
# Directory where the dockerfile is
build: ./product
# sourcedirectory:/directoryonhost
volumes:
- ./product:/usr/src/app
# localhostport:containerport
ports:
- 5001:80
website:
# gets from dockerhub
image: php:apache
volumes:
- ./website:/var/www/html
ports:
- 5000:80
depends_on:
- product-service
Run docker-compose file by running this command docker-compose up
. You can stop the services with CTRL + C
. The containers can be started up by using docker-compose up -d
This will allow you to still navigate in the terminal and you can stop it by docker-compose stop
.
docker ps -a
: List containers with name, ID, etcdocker stop d31a3b925d89
: Stop container with IDdocker system prune
: Remove stopped containers and dangling imagesdocker images
: List images with repository, image ID, etcdocker rmi e91727a11cd7
: Remove image with image IDdocker build -t webserver-image:v1 .
: Build image with name: web server-image version: v1docker run -d -p 5000:80 webserver-image:v1
: Run image in container port 5000 locally and port 80 on containerdocker-compose up
: Build image and run in container based on configuration in docker-compose.yaml filedocker-compose up --build
: To do a fresh build of images. Needed to update files.docker-compose up -d
: Create containers and -d
to launch in daemon mood (still navigate cmd)docker-compose stop
: Stop all containersMedium article](https://medium.com/myriatek/using-docker-to-run-a-simple-nginx-server-75a48d74500b)
FROM nginx:alpine
RUN mkdir /www
COPY dist/index.html /www/index.html
COPY dist/css /www/css
COPY dist/js /www/js
COPY dist/img /www/img
COPY nginx.conf /etc/nginx/nginx.conf
The first line defines our base image. The second line copies the content of the current directory into a particular location inside the container.
events {}
http {
server {
listen 80;
root /www;
index index.html;
location / {
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html;
}
}
}
version: '3.7'
services:
web-service:
build: ./
ports:
- 5000:80
volumes: ['./server/src:/app'] # To auto-reload new code from server/src to Docker app