我有兩個問題:
- Dockerfile 有兩個命令,添加組和用戶,都命名為 www,但沒有創建。
- 如何停止 docker-compose up -d 創建的容器。
我關注了這篇文章:
https ://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose
git clone https://github.com/laravel/laravel.git mylaravel9
編輯 docker-compose.yml 和 Dockerfile,然后
docker-compose up -d
瀏覽器“http://localhost”顯示,但有錯誤,一個有權限問題的日志檔案。這已經解決了,但并沒有真正解決。
碼頭工人-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:8.1.4-fpm
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 123456
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Dockerfile:
FROM php:8.1.4-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
第一個問題:
第一次做的時候
docker-compose up -d
它拉取東西,并在 Dockerfile 中運行命令。有問題,也解決了
failed to solve: executor failed running [/bin/sh -c docker-php-ext-install pdo_mysql mbstring zip exif pcntl]: exit code: 1
一個帖子說“mbstring”需要被洗掉。好的,Dockerfile 真的用上了。但是有兩個命令不起作用
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
因為
docker exec -it app bash
并在應用容器的外殼中
cd /var/www
ls -l
我看到組和所有者是第 1000 號,而不是 www。然后
cat /etc/passwd
The user and group of "www" doesn't exist! Why? I manually add the www group and user, and do chmod, the problem of log file permission is solved. But why www doesnt exist? The add commands are in the Dockerfile.
The second question
Exit the app shell, back to Ubuntu
docker ps
Shows three conatiners: php, nginx, mysql. But in docker interface(Windows 11), there is a container named by the folder mylaravel9.
docker stop mylaravel9
It says:
Error response from daemon: No such container: mylaravel9
So I can only stop the whole thing in the docker UI? If I want to use command, I have to stop the three containers? Is it?
uj5u.com熱心網友回復:
您顯示的設定中有兩個重大問題。
volumes:
- ./:/var/www
在 Dockerfile 中,您COPY --chown滿足于不同的用戶,但隨后此volumes:掛載隱藏了映像設定在/var/www目錄中所做的一切,并將其替換為主機中的內容。在容器內,您將看到主機的數字用戶 ID 和來自主機的可能不相關的代碼。我建議只洗掉這一行。
build: .
image: php:8.1.4-fpm
這種組合告訴 Compose 從其 Dockerfile 構建您的應用程式,然后將結果標記為原始php:8.1.4-fpm影像。當您重新運行docker-compose build時,它將從標記為 的內容開始php:8.1.4-fpm,這意味著您正在反復重新安裝您的應用程式。
如果您不打算將構建的鏡像推送到注冊表,請洗掉該image:行(如果您是,請使用構建鏡像的名稱和標簽,而不是基礎鏡像)。 docker pull php:8.1.4-fpm手動確保您擁有此基礎映像的“良好”副本。
在 Compose 專案的背景關系中,您通常不需要使用基本docker命令;大多數操作都有docker-compose包裝器。如果您想更新您的應用程式并重新啟動其容器,則應該足以
docker-compose build
docker-compose up -d
will 將重新創建更改的app容器,但不理會其他容器。如果您出于某種原因確實需要停止單個容器(“停止”是一種有點不尋常的狀態),那么docker-compose stop可以這樣做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457928.html
標籤:docker
上一篇:如何設定PhpStorm/WebStorm以在docker容器中使用Node.js?
下一篇:css中的邊框圖片
