docker-compose:创建数据库容器

时间:2017-02-02 21:24:16

标签: postgresql docker docker-compose dockerfile

我试图使用docker-compose创建一个postgresql docker容器,但似乎当我使用预先准备好的postgresql映像时,控制台永远不会返回主机,如果我按Ctrl + C容器最终被停止了。这是我得到的:

搬运工-compose.yml

version: '2'
services:
  database:
    container_name: database_server
    build:
      context: .
      dockerfile: Dockerfile_database
    image: database:tag
    expose:
      - "5432"

Dockerfile_database

FROM library/postgres
ADD init.sql /docker-entrypoint-initdb.d/

init.sql

CREATE USER usuario WITH PASSWORD 'senha';
CREATE DATABASE telesaude;
GRANT ALL PRIVILEGES ON DATABASE telesaude TO usuario;

并且在容器创建期间,控制台会显示一些postgresql日志,例如:

Building database
Step 1/2 : FROM library/postgres
 ---> 4023a747a01a
Step 2/2 : ADD init.sql /docker-entrypoint-initdb.d/
 ---> e264774d1f0b
Removing intermediate container 8df766b5a9f2
Successfully built e264774d1f0b
WARNING: Image for service database was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating database_server
Attaching to database_server
database_server | The files belonging to this database system will be owned by user "postgres".
database_server | This user must also own the server process.
database_server | 
database_server | The database cluster will be initialized with locale "en_US.utf8".
database_server | The default database encoding has accordingly been set to "UTF8".
database_server | The default text search configuration will be set to "english".
database_server | 
database_server | Data page checksums are disabled.
database_server | 
database_server | fixing permissions on existing directory /var/lib/postgresql/data ... ok
database_server | creating subdirectories ... ok
database_server | selecting default max_connections ... 100
database_server | selecting default shared_buffers ... 128MB
database_server | selecting dynamic shared memory implementation ... posix
database_server | creating configuration files ... ok
database_server | running bootstrap script ... ok
database_server | performing post-bootstrap initialization ... ok
database_server | syncing data to disk ... 
database_server | WARNING: enabling "trust" authentication for local connections
database_server | You can change this by editing pg_hba.conf or using the option -A, or
database_server | --auth-local and --auth-host, the next time you run initdb.
database_server | ok
database_server | 
database_server | Success. You can now start the database server using:
database_server | 
database_server |     pg_ctl -D /var/lib/postgresql/data -l logfile start
database_server | 
database_server | ****************************************************
database_server | WARNING: No password has been set for the database.
database_server |          This will allow anyone with access to the
database_server |          Postgres port to access your database. In
database_server |          Docker's default configuration, this is
database_server |          effectively any other container on the same
database_server |          system.
database_server | 
database_server |          Use "-e POSTGRES_PASSWORD=password" to set
database_server |          it in "docker run".
database_server | ****************************************************
database_server | waiting for server to start....LOG:  database system was shut down at 2017-02-02 21:15:46 UTC
database_server | LOG:  MultiXact member wraparound protections are now enabled
database_server | LOG:  database system is ready to accept connections
database_server | LOG:  autovacuum launcher started
database_server |  done
database_server | server started
database_server | ALTER ROLE
database_server | 
database_server | 
database_server | /docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
database_server | CREATE ROLE
database_server | CREATE DATABASE
database_server | GRANT
database_server | 
database_server | 
database_server | LOG:  received fast shutdown request
database_server | LOG:  aborting any active transactions
database_server | waiting for server to shut down...LOG:  autovacuum launcher shutting down
database_server | .LOG:  shutting down
database_server | LOG:  database system is shut down
database_server |  done
database_server | server stopped
database_server | 
database_server | PostgreSQL init process complete; ready for start up.
database_server | 
database_server | LOG:  database system was shut down at 2017-02-02 21:15:49 UTC
database_server | LOG:  MultiXact member wraparound protections are now enabled
database_server | LOG:  database system is ready to accept connections
database_server | LOG:  autovacuum launcher started
(console never returns to host after this)

我应该怎么做才能使容器继续运行/启动并且控制台返回主机? 我必须使用docker-compose。不能使用docker run -d ...

1 个答案:

答案 0 :(得分:1)

要使用compose在后台运行容器,请使用分离选项:

docker-compose up -d