Como instalar MySQL em um container Docker?
Abr 02, 2019
ubuntu code snippets docker knowledge base
Instalar Docker no Ubuntu
Confira este guia prático para instalar o Docker no Ubuntu: Como instalar Docker no Ubuntu?.
Vamos rodar um container MySQL em segundo plano. A senha do usuário root é "test".
docker run --rm -it -e MYSQL_ROOT_PASSWORD=test -d mysql
-d: Executa em background e mostra o ID do container.--rm: Remove o container automaticamente ao parar.
Ver containers em execução
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ee2e6c8174d mysql "docker-entrypoint.s…" 9 seconds ago Up 7 seconds 3306/tcp, 33060/tcp condescending_snyder
Acessar o shell do MySQL
docker exec -it 8ee2e6c8174d mysql -u root -ptest
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15 MySQL Community Server - GPL
mysql>
Detalhes do container
Para ver configurações de rede, volumes e mais:
docker inspect 8ee2e6c8174d
Exemplo de saída (resumida):
[
{
"Id": "8ee2e6c8174d4138026f1d54addf9aadc7fbe22727c7f1fb9ca996ce5f7601b2",
"Created": "2019-03-28T20:53:46.84185469Z",
"State": {
"Status": "running",
"Running": true
},
"Networks": {
"bridge": {
"IPAddress": "172.17.0.2",
"Gateway": "172.17.0.1"
}
}
}
]
Conectar no MySQL pelo IP
Use o IP do container:
mysql -u root -ptest -h 172.17.0.2
Listar todos os containers (ativos e parados)
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ee2e6c8174d mysql "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 3306/tcp, 33060/tcp condescending_snyder
913f33f5a848 mysql "docker-entrypoint.s…" 12 minutes ago Exited (0) 11 minutes ago jovial_dubinsky
b36cf8b3b664 mysql "docker-entrypoint.s…" 22 minutes ago Exited (1) 22 minutes ago cocky_mcnulty
Ver todas as imagens
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 7bb2586065cd 45 hours ago 477MB