|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: How to build and push a container image |
| 4 | + description: How to build and push a container image based on a Dockerfile for deploying containerized applications with Scaleway Serverless Containers. |
| 5 | +content: |
| 6 | + h1: How to build and push a container image |
| 7 | + paragraph: How to build and push a container image based on a Dockerfile for deploying containerized applications with Scaleway Serverless Containers. |
| 8 | +tags: create dockerfile containerize application deployment scaleway |
| 9 | +dates: |
| 10 | + validation: 2025-04-02 |
| 11 | + posted: 2021-04-02 |
| 12 | +categories: |
| 13 | + - serverless |
| 14 | + - containers |
| 15 | +--- |
| 16 | + |
| 17 | +This page explains how to create a simple Dockerfile to containerize your applications for deployment using Scaleway Serverless Containers. |
| 18 | + |
| 19 | +<Macro id="requirements" /> |
| 20 | + |
| 21 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 22 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 23 | +- Installed [Docker engine](https://docs.docker.com/engine/install/) or the Docker daemon locally |
| 24 | +- Created a [Scaleway Registry namespace](/container-registry/how-to/create-namespace/) |
| 25 | +- A valid [API key](/iam/how-to/create-api-keys/) |
| 26 | + |
| 27 | +## How to create a Dockerfile |
| 28 | + |
| 29 | +1. In a new folder, create a file named `Dockerfile`. |
| 30 | + |
| 31 | +2. Add the following content to your Dockerfile, adjusting the base image and commands according to your application: |
| 32 | + |
| 33 | +<Tabs id="dockerfile-examples"> |
| 34 | +<TabsTab label="Nginx"> |
| 35 | +```dockerfile |
| 36 | +# Use the official Nginx image |
| 37 | +FROM nginx:alpine |
| 38 | + |
| 39 | +# Create simple HTML content directly in the Dockerfile |
| 40 | +RUN echo "<!DOCTYPE html><html><head><title>Demo</title></head><body><h1>Hello Scaleway Serverless Containers!</h1><p>Served by Nginx</p></body></html>" > /usr/share/nginx/html/index.html |
| 41 | + |
| 42 | +# For documentation purposes |
| 43 | +EXPOSE 8080 |
| 44 | + |
| 45 | +# Modify Nginx configuration to listen on 8080 |
| 46 | +RUN sed -i 's/listen\(.*\)80;/listen 8080;/' /etc/nginx/conf.d/default.conf |
| 47 | + |
| 48 | +# Start Nginx |
| 49 | +CMD ["nginx", "-g", "daemon off;"] |
| 50 | +``` |
| 51 | +</TabsTab> |
| 52 | +<TabsTab label="NodeJS"> |
| 53 | +```dockerfile |
| 54 | +# Use the official Node.js slim image |
| 55 | +FROM node:22-slim |
| 56 | + |
| 57 | +# Create app directory |
| 58 | +WORKDIR /usr/src/app |
| 59 | + |
| 60 | +# Create package.json and simple Express app directly in Dockerfile |
| 61 | +RUN echo '{"name":"scaleway-serverless","version":"1.0.0","description":"","main":"server.js","scripts":{"start":"node server.js"},"dependencies":{"express":"^5"}}' > package.json && \ |
| 62 | + npm install && \ |
| 63 | + echo "const express = require('express');\nconst app = express();\nconst port = process.env.PORT || 8080;\n\napp.get('/', (req, res) => {\n res.send('<!DOCTYPE html><html><body><h1>Hello from Scaleway Serverless!</h1></body></html>');\n});\n\napp.listen(port, () => {\n console.log(`Server running on port \${port}`);\n});" > server.js |
| 64 | + |
| 65 | +# Start the application |
| 66 | +CMD ["npm", "start"] |
| 67 | +``` |
| 68 | +</TabsTab> |
| 69 | +<TabsTab label="Python Flask"> |
| 70 | +```dockerfile |
| 71 | +# Use the official Python slim image |
| 72 | +FROM python:3.13-slim |
| 73 | + |
| 74 | +# Install Flask |
| 75 | +RUN pip install flask gunicorn |
| 76 | + |
| 77 | +# Create a simple Flask app directly in the Dockerfile |
| 78 | +RUN echo "from flask import Flask\napp = Flask(__name__)\n\n@app.route('/')\ndef hello():\n return '<!DOCTYPE html><html><body><h1>Hello from Flask on Scaleway Serverless!</h1></body></html>'\n\nif __name__ == '__main__':\n app.run(host='0.0.0.0', port=8080)" > app.py |
| 79 | + |
| 80 | +# Run the app with Gunicorn |
| 81 | +CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app |
| 82 | +``` |
| 83 | +</TabsTab> |
| 84 | +</Tabs> |
| 85 | + |
| 86 | +## How to build and push your image from your Dockerfile |
| 87 | + |
| 88 | +1. Open a terminal and navigate to the directory containing your Dockerfile. |
| 89 | + |
| 90 | +2. Run the following command to build your Docker image: |
| 91 | + |
| 92 | + ```bash |
| 93 | + docker build -t my-application . |
| 94 | + ``` |
| 95 | + |
| 96 | +3. Run the command below to log in to your Scaleway account in the terminal. Make sure that you replace the placeholder values with your own: |
| 97 | + |
| 98 | + ``` |
| 99 | + docker login rg.fr-par.scw.cloud/your-container-registry-namespace -u nologin --password-stdin <<< "$SCW_SECRET_KEY" |
| 100 | + ``` |
| 101 | + |
| 102 | +4. Tag your Docker image so it matches your Scaleway registry's format: |
| 103 | + |
| 104 | + ``` |
| 105 | + docker tag my-application:latest rg.fr-par.scw.cloud/your-container-registry-namespace/my-application:latest |
| 106 | + ``` |
| 107 | + |
| 108 | +5. Push the Docker image to the Scaleway Container Registry: |
| 109 | + |
| 110 | + ``` |
| 111 | + docker push rg.fr-par.scw.cloud/your-container-registry-namespace/my-application:latest |
| 112 | + ``` |
| 113 | + |
| 114 | +You can now access your container image from the [Scaleway Container Registry](https://console.scaleway.com/registry/namespaces), and [deploy a Serverless Container](/serverless-containers/how-to/deploy-container/#deploy-from-an-external-container-registry) from this image. |
0 commit comments