简体   繁体   中英

Host Dockerized Django project on AWS

I have a Django project which is working fine on my local machine. I want to host the same on AWS, but confused on what service to use and what is the best practice to so. Do I use EC2, create a ubuntu instance on it and install Docker or use ECS ?

What is the best practice to transfer my django project to AWS. Do I create a repository on Docker hub ?

Please help me explain the best workflow on this.

My docker-compose file looks like this:

version: '3'

services:

  db:
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_DATABASE=tg_db
      - MYSQL_ROOT_PASSWORD=password
    volumes:
      - ./dbdata:/var/lib/mysql

  web:
    build: .
    command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Thanks!

UPDATE (Steps I took for deployment) Dockerfile :

# Start with a python image
FROM python:3

# Some stuff that everyone has been copy-pasting
# since the dawn of time.
ENV PYTHONUNBUFFERED 1

# Install things
RUN apt-get update

# Make folders and locations for project

RUN mkdir /code
COPY . /code
WORKDIR /code/project/t_backend

# Install requirements

RUN pip install -U pip
RUN pip install -Ur requirements.txt

I used sudo docker-compose up -d and project is running on local

Now I pushed my tg_2_web:latest on ECR . Where does the database and Apache containers come in action.

Do I have to create a separated repository for both mysql database and apache container.

How will I connect all the containers using ECS ?

Thanks !

The answer to this question can be really wide but just to give you a heads up on what all processes it is supposed to go through -

  1. Packaging Images

    • You create a docker image by using writing Dockerfile which actually copies your Python Django source code & installs all the dependencies.
    • This can either be done locally of you can use any CI/CD tools for the same.
  2. Storing Images

    • This is the part where you will push & store your Docker image. All the packaged images will be pushed in this step.
    • This could be any registry from where EC2 instances can fetch the docker image, preferably ECR but you can opt for dockerhub as well. In case of dockerhub, you need to store your credentials into S3.
  3. Deploying images

    • In this part, you will be deploying the images to EC2 instances.
    • You can use various services depending on your requirement like ECS, ElasticBeanstalk multicontainer or maybe Fargate(relatively new).
    • ECS - Most preferred way of deployment but you need to manage clusters & resources by yourself. Images have to be defined in a task definition which is a JSON file.
      Beanstalk Multi Container - Relatively new to ECS, uses ECS in the background to deploy your docker images to the clusters. You do not have to worry about resources, just feed a JSON file to your environment & rest is taken care by Beanstalk.
      Fargate - Manage or deploy your containers without worrying about your clusters/managers etc. Quite new, never got a chance to have a look into it.

Ref -
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_ecs.html
https://aws.amazon.com/fargate/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM